const path = require("path");
const webpackBuildTool = require("./build-tools/webpack-tool");
const rollupBuildTool = require("./build-tools/rollup-tool");
const CIRCULAR_DEPENDENCY = ["util", "stream", "buffer"]; //循环依赖的模块
const NO_CIRCULAR_DEPENDENCY = [
  "events",
  "path",
  "process",
  "querystring",
  "timers",
  "url",
  "string_decoder",
  "net",
]; //不存在循环依赖的模块

function main() {
  Promise.all(
    NO_CIRCULAR_DEPENDENCY.map((v) =>
      rollupBuildTool({
        input: path.resolve(__dirname, `../polyfills/lib/${v}/index.js`),
        output: {
          file: path.resolve(__dirname, `../polyfills/dist/${v}.js`),
        },
      })
    )
  )
    .then((res) => {
      console.log("打包成功");
    })
    .catch((res) => {
      console.log("打包失败");
    });
  Promise.all(
    CIRCULAR_DEPENDENCY.map((v) =>
      webpackBuildTool({
        entry: path.resolve(
          __dirname,
          v === "stream"
            ? "../polyfills/lib/stream/lib/ours/index.js"
            : `../polyfills/lib/${v}/index.js`
        ),
        output: {
          path: path.resolve(__dirname, "../polyfills/dist"),
          filename: `${v}.js`,
        },
      })
    )
  )
    .then((res) => {
      console.log("打包成功");
    })
    .catch((res) => {
      console.log("打包失败", res);
    });
}

main();
// const compiler = webpack(
//   {
//     entry: path.resolve(__dirname, "../polyfills/lib/util/index.js"),
//     mode: "production",
//     experiments: {
//       outputModule: true,
//     },
//     devtool: false,
//     output: {
//       path: path.resolve(__dirname, "dist"),
//       filename: "stream.js",
//       library: { type: "module" },
//     },
//     externals: [
//       "buffer",
//       "process",
//       "events",
//       "@ohos.util",
//       "@ohos.process",
//       "@ohos.hilog",
//       "@ohos.util",
//     ],
//     resolve: {
//       extensions: [".js", ".ts", ".esm.js"],
//     },
//   },
//   (err, stats) => {
//     console.log(err);
//   }
// );

// compiler.run(() => {});
// 3224674803671562146
// IPhone 13 Pro_Negro Noche
